<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/10/30
 * Time: 下午1:49
 */

namespace App\Models;

use App\Helpers\Log;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class RechargeOrder extends Model
{
    protected $table = 'recharge_order';

    public $guarded = [];

    public $appends = ['status_name','recharge_mode_name'];

    // 0待支付，1已支付，2待充值，3充值完成，-1取消
    const STATUS_INIT       = 0;
    const STATUS_PAID       = 1;
    const STATUS_PROCESSING = 2;
    const STATUS_DONE       = 3;
    const STATUS_CANCELLED  = -1;
    const STATUS_REFUND     = 7;

    // 1套餐充值,2直接充值
    const TYPE_RECHARGE = 1;
    const TYPE_DIRECT   = 2;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function oilCard()
    {
        return $this->belongsTo(OilCard::class);
    }

    public function detail()
    {
        return $this->hasMany(RechargeOrderDetail::class);
    }

    public function recharge()
    {
        return $this->belongsTo(Recharge::class);
    }

    public function coupon()
    {
        return $this->belongsTo(UserCoupon::class,'coupon_id','id');
    }

    public function pay()
    {
        return $this->belongsTo(Pay::class);
    }

    public function getRechargeModeNameAttribute()
    {
        switch($this->recharge_mode){
            case 1:
                return '自动';
            case 2:
                return '手动';
            case 3:
                return '混合';
            default:
                return '未定义';
        }
    }

    public function getStatusNameAttribute()
    {
        // 0待支付，1已支付，2待充值，3充值完成，-1取消
        switch ($this->status) {
            case 1:
                return '已支付';
            case 2:
                return '进行中';
            case 3:
                return '充值完成';
            case 7:
                return '已退款';
            case -1:
                return '已取消';
            case 0:
                return '待支付';
            default:
                return '未定义';
        }
    }


    public static function rechargeOrderRefresh(){
        //获取领取油卡待支付订单
        $oilcard = CompanyOilCard::where("status",1)->get();
        if ($oilcard->count()>0) {
            foreach ($oilcard as $item){
                $bool = true;
                $order = RechargeOrder::where("card_no",$item->card_no)->where("shop_order_id",">",0)->get();
                if ($order->count()>0){
                    foreach ($order as $value){
                        if (in_array($value->status,[0,1,2,3,4])){
                            $bool = false;
                        }else{
                            $bool = true;
                        }
                    }
                }
                if ($bool==true){
                    $item->status = 0;
                    $item->used_at = (NULL);
                    $item->user_id = 0;
                    $item->save();
                    OilCard::where("card_no",$item->card_no)->update(["status"=>0,"deleted_at"=>Carbon::now()]);
                }
            }
        }
    }
}